﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data
{
    /// <summary>
    /// EntityFragmentPICProvider : Persist In Cache data provider
    /// </summary>
    /// <typeparam name="I">Object to provide interface type</typeparam>
    /// <typeparam name="C">Object to provide concrete class type</typeparam>
    public static class EntityFragmentPICProvider<T>
            where T : IEntityFragmentData
    {
        public static string[] GetFragments (Dictionary<string, T> dataObjects, string parentId, GetDataMode mode)
        {
            if (parentId == null)
                throw new ArgumentNullException();
            var query = (from d in dataObjects.Values where d.OwnerId == parentId select d.GlobalId);
            if (query.Count() == 0)
                if ((mode & GetDataMode.Find) != GetDataMode.Find)
                    throw new DataNotFoundException("parentId", parentId);
                else
                    return null;
            else
                return query.ToArray();
        }


        public static int FragmentsCount(Dictionary<string, T> dataObjects, string parentId)
        {
            return dataObjects.Values.Count(p => p.OwnerId == parentId);
        }

    }
}
